package cc.wsyw126.java.nowcoder.sword_offer;

/**
 * Created by dp on 17/3/27.
 */
public class Power {
    public double power(double base, int exponent) {
        double result = 0.0;
        if (exponent < 0) {
            double v = doPower(base, -exponent);
            result = 1.0/v;
        } else {
            double v = doPower(base, exponent);
            result = v;
        }
        return result;
    }
    private double doPower(double base, int exponent) {
        if (exponent == 0) return 1;
        if (exponent == 1) return base;
        double result = 1.0;
        for (; exponent > 0; exponent = exponent >> 1) {
            if ((exponent & 0x1) == 0x1) {
                result *= base;
            }
            base *= base;
        }
        return result;
    }

    public static void main(String[] args) {
        Power power = new Power();
        double power1 = power.power(10, -2);
        System.out.println("power1 = " + power1);
    }
}
